package com.ciban.smarthome.module.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.RegexUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.engine.PorjectorEngine;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.entity.Equip_Projector;

public class E_ProjectorModifyActivity extends BaseActivity implements
		OnClickListener {
	private EditText et_no, et_pmno, et_name;
	private Button bt_cancle, bt_save;

	private Equip_Projector projector;
	private boolean isEdit;

	private PorjectorEngine engine;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_equip_projector_modify);

		initData();

		initViews();
	}

	private void initData() {
		if (getIntent().hasExtra("projector")) {
			isEdit = true;
			projector = (Equip_Projector) getIntent().getExtras().get("projector");
		}else if(getIntent().hasExtra("type")){
			projector=new Equip_Projector();
			projector.setType(getIntent().getIntExtra("type",-1));
		}

		engine=new PorjectorEngine(this);
	}

	private void initViews() {
		et_name = (EditText) findViewById(R.id.projector_add_name);
		et_no = (EditText) findViewById(R.id.projector_add_no);
		et_pmno = (EditText) findViewById(R.id.projector_add_pmno);

		bt_cancle = (Button) findViewById(R.id.projector_add_cancle);
		bt_save = (Button) findViewById(R.id.projector_add_save);

		if (isEdit) {
			et_name.setText(projector.getName());
			et_no.setText(projector.getSerial_no() + "");
			et_pmno.setText(projector.getPro_screen_serialno() == -1 ? "" : (projector
					.getPro_screen_serialno() + ""));
		}

		setListeners();

	}

	private void setListeners() {
		bt_cancle.setOnClickListener(this);
		bt_save.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.projector_add_cancle:
			finish();
			break;
		case R.id.projector_add_save:
			addPro();
			break;
		}
	}

	private void addPro() {
		String no = et_no.getText().toString().trim();
		if (StringUtils.isEmpty(no) || !RegexUtils.isNumStr(no)) {
			displayShort("编号填写错误");
			return;
		}
		String pmno = et_pmno.getText().toString().trim();
		if (StringUtils.isEmpty(pmno)) {
			pmno = "-1";
		}
		String name = et_name.getText().toString().trim();
		if (StringUtils.isEmpty(name)) {
			displayShort("请输入名称");
			return;
		}

		if (projector == null)
			projector = new Equip_Projector();
		projector.setSerial_no(Integer.parseInt(no));
		projector.setPro_screen_serialno(Integer.parseInt(pmno));
		projector.setName(name);
		projector.setArea_id(baseApp.getNowArea().getId());

		boolean isSuccess = false;
		if (isEdit) {
			isSuccess=engine.updateProjector(projector);
		} else {
			isSuccess=engine.addPorjector(projector);
		}
		if (!isSuccess)
			return;
		displayShort("操作成功");
		Intent data = new Intent();
		data.putExtra("projector", projector);
		setResult(RESULT_OK, data);
		finish();
	}
}
